import os
from openai import OpenAI

client = OpenAI(
    # 若没有配置环境变量，请用百炼API Key将下行替换为：api_key="sk-xxx",
    api_key="sk-",
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
    model="qwen-plus",  # 此处以qwen-plus为例，可按需更换模型名称。模型列表：https://help.aliyun.com/zh/model-studio/getting-started/models
    messages=[
        {"role": "system", "content": "我是一个AI智能助手，可以回答你的任何问题。"},
        {"role": "user", "content": "你是什么大模型"},
    ],
    stream=True,  # 启用流式输出
    stream_options={"include_usage": True}  # 启用流式输出时，返回usage信息
)
for chunk in completion:
    if chunk.choices and len(chunk.choices) > 0:
        print(chunk.choices[0].delta.content)
